﻿var geocoder;
var map;
var marker;
var expandControlDiv;
var scrollPosition = 0;

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-14.826691, -47.9218204);
    var myOptions = { zoom: 3, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP, scrollwheel: false }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    //Add controle de ampliar mapa
    expandControlDiv = document.createElement('DIV');
    var expandControl = new ExpandControl(expandControlDiv, map);
    expandControlDiv.index = 1;
    map.controls[google.maps.ControlPosition.TOP_RIGHT].push(expandControlDiv);

}

function codeAddress(address, zoom) {
    if (marker)
        marker.setMap(null);
    //var address = document.getElementById("address").value;
    geocoder.geocode({ 'address': address },
                        function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK) {
                                map.setCenter(results[0].geometry.location);
                                map.setZoom(zoom);
                                document.getElementById("ContentPlaceHolder1_txtLatitude").value = results[0].geometry.location.lat();
                                document.getElementById("ContentPlaceHolder1_txtLongitude").value = results[0].geometry.location.lng();
                                marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, draggable: true });
                                google.maps.event.addListener(marker, 'click', function () { map.setCenter(marker.position); map.setZoom(16); });
                                google.maps.event.addListener(marker, 'dragend', function (event) {
                                    document.getElementById("ContentPlaceHolder1_txtLatitude").value = event.latLng.lat();
                                    document.getElementById("ContentPlaceHolder1_txtLongitude").value = event.latLng.lng();
                                });
                            }
                            else {
                                alert("Geocode was not successful for the following reason: " + status);
                            }
                        });
}

function centerMap(lat, lng, zoom) {

    var latlng = new google.maps.LatLng(lat, lng);
    marker = new google.maps.Marker({ map: map, position: latlng, draggable: true });

    google.maps.event.addListener(marker, 'click', function () { map.setCenter(latlng); map.setZoom(zoom); });
    google.maps.event.addListener(marker, 'dragend', function (event) {
        document.getElementById("ContentPlaceHolder1_txtLatitude").value = event.latLng.lat();
        document.getElementById("ContentPlaceHolder1_txtLongitude").value = event.latLng.lng();
    });


    map.setCenter(latlng);
    map.setZoom(zoom);
}

function centerAddress() {

    var address = "";
    address = document.getElementById("ContentPlaceHolder1_txtLogradouro").value;
    address = address + ", " + document.getElementById("ContentPlaceHolder1_txtNumero").value;
    //address = address + ", " + document.getElementById("ContentPlaceHolder1_txtBairro").value;
    address = address + ", " + document.getElementById("ContentPlaceHolder1_ddlCidade").value;
    address = address + ", " + document.getElementById("ContentPlaceHolder1_ddlEstado").value;
    codeAddress(address, 15);
}

function centerAddressFornecedor() {

    var address = "";
    address = document.getElementById("ContentPlaceHolder1_txtLogradouro").value;
    address = address + ", " + document.getElementById("ContentPlaceHolder1_txtNumero").value;
    //address = address + ", " + document.getElementById("ContentPlaceHolder1_txtBairro").value;
    address = address + ", " + document.getElementById("ContentPlaceHolder1_txtCidade").value;
    address = address + ", " + document.getElementById("ContentPlaceHolder1_ddlEstado").value;
    codeAddress(address, 15);
}


function ExpandControl(controlDiv, map) {
    // Set CSS styles for the DIV containing the control   
    // Setting padding to 5 px will offset the control   
    // from the edge of the map   
    controlDiv.style.paddingTop = '5px';
    controlDiv.id = "expandMap";

    // Set CSS for the control border   
    var controlUI = document.createElement('DIV');
    controlUI.title = 'Click para ampliar';
    $(controlUI).css("background-color", "#FEFEFE");
    $(controlUI).css("border-style", "solid");
    $(controlUI).css("border-width", "1px");
    $(controlUI).css("border-color", "#A9BBDF");
    $(controlUI).css("cursor", "pointer");
    $(controlUI).css("text-align", "center");
    controlDiv.appendChild(controlUI);

    // Set CSS for the control interior   
    var controlText = document.createElement('DIV');
    controlText.innerHTML = 'Ampliar';
    $(controlUI).css("font-family", "Arial,sans-serif");
    $(controlUI).css("font-size", "12px");
    $(controlUI).css("padding-left", "6px");
    $(controlUI).css("padding-right", "6px");
    $(controlUI).css("padding-top", "4px");
    $(controlUI).css("padding-bottom", "4px");

    if ($.browser.msie)
        if ($.browser.version == "8.0" || $.browser.version == "9.0") {
            $(controlUI).css("padding-top", "2px");
            $(controlUI).css("padding-bottom", "2px");
        }

    controlUI.appendChild(controlText);

    google.maps.event.addDomListener(controlUI, 'click', function () {
        if ($("#expandMap > div >div").text() == "Ampliar") {
            scrollPosition = $(window).scrollTop();
            $("#map_canvas").css("position", "absolute");
            $("#map_canvas").css("left", "0px");
            $("#map_canvas").css("top", "0px");
            $("#map_canvas").css("z-index", "99999");
            google.maps.event.trigger(map, "resize");
            $("body").css("height", "200px");
            $("body").css("overflow", "hidden");
            $("#expandMap > div >div").text("Diminuir");
            $(window).scrollTop(0);
        }
        else {
            $("#map_canvas").css("position", "relative");
            $("body").css("height", "auto");
            $("body").css("overflow", "visible");
            $("#expandMap > div >div").text("Ampliar");
            google.maps.event.trigger(map, "resize");
            $(window).scrollTop(scrollPosition);
        }

        if (marker != undefined)
            map.setCenter(marker.position);
    });

} 